package com.muyu.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.muyu.common.core.constant.ImConstants;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.im.ImUtils;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.system.Vo.DmShoppingStoreVO;
import com.muyu.system.danmo.dto.AddRegistrationIdDTO;
import com.muyu.system.domain.DmMessageInfo;
import com.muyu.system.domain.DmShoppingStore;
import com.muyu.system.dto.CloseStoreDTO;
import com.muyu.system.dto.DmShoppingStoreDTO;
import com.muyu.system.service.IDmShoppingStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 商家信息Controller
 *
 * @author ruoyi
 * @date 2025-01-13
 */
@RestController
@RequestMapping("/store")
public class DmShoppingStoreController extends BaseController {
    @Autowired
    private IDmShoppingStoreService dmShoppingStoreService;

    /**
     * 查询商家信息列表
     */
    //@RequiresPermissions("system:store:list")
    @GetMapping("/list")
    public Result list(DmShoppingStore dmShoppingStore) {
        startPage();
        List<DmShoppingStore> list = dmShoppingStoreService.selectDmShoppingStoreList(dmShoppingStore);
        return getDataTable(list);
    }

    /**
     * 导出商家信息列表
     */
    //@RequiresPermissions("system:store:export")
    @Log(title = "商家信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmShoppingStore dmShoppingStore) {
        List<DmShoppingStore> list = dmShoppingStoreService.selectDmShoppingStoreList(dmShoppingStore);
        ExcelUtil<DmShoppingStore> util = new ExcelUtil<DmShoppingStore>(DmShoppingStore.class);
        util.exportExcel(response, list, "商家信息数据");
    }

    /**
     * 获取商家信息详细信息
     */
    //@RequiresPermissions("system:store:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return Result.success(dmShoppingStoreService.selectDmShoppingStoreById(id));
    }

    /**
     * 根据userId获取商家信息详细信息
     */
    //@RequiresPermissions("system:store:query")
    @GetMapping("/getInfoByUserId")
    public Result<DmShoppingStoreVO> getInfoByUserId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.warn("请登录");
        }
        Long userId = loginUser.getUserid();
        return Result.success(dmShoppingStoreService.selectDmShoppingStoreByUserId(userId));
    }

    /**
     * 新增商家信息
     */
    //@RequiresPermissions("system:store:add")
    @Log(title = "商家信息", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody @Valid DmShoppingStoreDTO dmShoppingStore) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        dmShoppingStore.setUserId(loginUser.getUserid());
        return Result.success(dmShoppingStoreService.insertDmShoppingStore(dmShoppingStore), "商铺创建成功");
    }

    /**
     * 发送验证码
     * @param tel
     * @return
     */
    @GetMapping("/send/code/{tel}")
    public Result<String> sendCode(@PathVariable("tel") String tel){
        dmShoppingStoreService.sendCode(tel);
        return Result.success("发送成功!");
    }

    /**
     * 修改商家信息
     */
    //@RequiresPermissions("system:store:edit")
    @Log(title = "商家信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DmShoppingStore dmShoppingStore) {
        return Result.success(dmShoppingStoreService.updateDmShoppingStore(dmShoppingStore), "修改成功");
    }

    /**
     * 删除商家信息
     */
    //@RequiresPermissions("system:store:remove")
    @Log(title = "商家信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return Result.success(dmShoppingStoreService.deleteDmShoppingStoreByIds(ids));
    }

    /**
     * 发送聊天消息
     */
    @PostMapping("/sendMessage")
    public Result sendMessage(@RequestBody DmMessageInfo dmMessageInfo) {
        //组装senderData信息
        JSONObject senderData = new JSONObject();
        senderData.put("avatar", dmMessageInfo.getSenderAvatar());
        senderData.put("nickname", dmMessageInfo.getSenderNickName());

        //组装接受者消息
        JSONObject receiverData = new JSONObject();
        JSONObject toData = new JSONObject();
        toData.put("avatar", dmMessageInfo.getReceiverAvatar());
        toData.put("name", dmMessageInfo.getReceiverNickName());
        receiverData.put("data", toData);
        receiverData.put("type", ImConstants.TYPE_PRIVATE);
        receiverData.put("id", dmMessageInfo.getReceiverId());

        //组装通知栏信息
        JSONObject notification = new JSONObject();
        notification.put("title", ImConstants.MESSAGE_TITLE);
        notification.put("body", dmMessageInfo.getContent());

        //组装整体消息
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appkey", ImConstants.APP_KEY);
        jsonObject.put("senderId", dmMessageInfo.getSenderId());
        jsonObject.put("senderData", senderData);
        jsonObject.put("to", receiverData);
        jsonObject.put("type", ImConstants.MESSAGE_TEXT);
        jsonObject.put("payload", dmMessageInfo.getPayload());
        jsonObject.put("notification", notification);

        return ImUtils.sendImMessage(jsonObject);
    }

    /**
     * 关闭店铺
     * @param closeStoreDTO
     * @return
     */
    @PostMapping("/close/store")
    public Result<String> closeStore(@RequestBody @Valid CloseStoreDTO closeStoreDTO) {
        dmShoppingStoreService.closeStore(closeStoreDTO);
        return Result.success();
    }


    /**
     * 开启店铺
     * @param closeStoreDTO
     * @return
     */
    @PostMapping("/open/store")
    public Result<String> openStore(@RequestBody @Valid CloseStoreDTO closeStoreDTO) {
        dmShoppingStoreService.openStore(closeStoreDTO);
        return Result.success();
    }


    /**
     * 注销商铺
     * @param closeStoreDTO
     * @return
     */
    @PostMapping("/cancel/store")
    public Result<String> cancelStore(@RequestBody @Valid CloseStoreDTO closeStoreDTO) {
        dmShoppingStoreService.cancelStore(closeStoreDTO);
        return Result.success();
    }


}
